package com.link.base.base.autoconfig.autoconfig.controller;

import com.link.base.base.autoconfig.autoconfig.service.LnkInnerFieldService;
import com.link.base.base.autoconfig.autoconfig.model.LnkInnerField;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.controller.BasicControllerException;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.modules.imports.ImportReader;
import com.link.core.modules.imports.ImportsFactory;
import com.link.core.util.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自动配置——对象字段
 *
 * @author 姚彭健
 * @date 2019-12-16 16:41
 */
@Controller("LnkInnerFieldController")
@RequestMapping("/link/LnkInnerField")
public class LnkInnerFieldController extends BasicController<LnkInnerField> {

    @Resource
    private LnkInnerFieldService service;

    private static final Logger logger = LogManager.getLogger(LnkInnerFieldController.class);

    @Override
    public BasicService<LnkInnerField> getBasicService() throws Exception {
        return service;
    }

    /**
     * 重写父类beforeUpsert方法校验表字段长度
     *
     * @author 姚彭健
     * @date 2019-07-17
     * @param entity 要校验的字段
     * @param request request
     * @throws Exception 校验异常
     *
     */
    @Override
    public void beforUpsert(LnkInnerField entity, HttpServletRequest request) throws Exception {
        // 校验表字段长度是否超过20
        boolean checkPassed = StringUtils.checkTable(entity.getName(), 20);
        if (!checkPassed) {
            throw new ServiceException("AUTOCONFIG-001");
        }
    }

    /**
     * 导入
     */
    @Override
    @RequestMapping(value = "/importData")
    @ResponseBody
    @Transactional(rollbackFor = BasicControllerException.class)
    public Map<String, Object> importData(LnkInnerField entity, QueryParams qps, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        //logger.info("导入列表。。。");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            ImportReader importReader = ImportsFactory.newInstance(qps, request);
            List<LnkInnerField> list = importReader.readList(LnkInnerField.class);
            if (list == null) {
                result.put("success", true);
                result.put("total", 0);
            } else {
                for (int i = 0; list != null && i < list.size(); i++) {
                    LnkInnerField record = list.get(i);
                    record.setParentid(qps.getImportParentId());
                    getBasicService().insert(record);
                }
                result.put("success", true);
                result.put("total", list.size());
            }

        } catch (Exception e) {
            logger.error("LnkInnerFieldController捕获异常:" + e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @Override
    public void beforQueryAllOrExample(LnkInnerField entity, HttpServletRequest request) throws Exception {
        super.beforQueryAllOrExample(entity, request);
        if (null == entity.getParentid()) {
            entity.setParentid(0L);
        }
    }
}
